// import { IChartConfig } from './../Charts/Charts';

import {ChartConfigMgr} from "../Charts/Charts";
import {DataSourceMgr} from "../dataSource/dataSource";
export const EffectConfigMap = new WeakMap<ChartConfigMgr<any>|DataSourceMgr|StateNode<any>,Array<()=>void>>();

export class StateNode<T>{
    private current:T;
    constructor(node:T){
        this.current = node;
    }
    get value(){
        return this.current;
    }
    set(node:T){
        this.current = node;
    }
}

export default function effect(fn: ()=>void,effectTarget: Array<ChartConfigMgr<any>|DataSourceMgr|undefined|StateNode<any>>){
    if(!effectTarget || effectTarget.length==0){
        console.error('effect必须依赖ChartConfigMgr或DataSourceMgr对象进行监听');
        return;
    }
    effectTarget.forEach(target=>{
        if(target){
            if((EffectConfigMap.get(target)||[]).length){
                const targets =  EffectConfigMap.get(target) || [];
                targets.push(fn);
            }else{
                EffectConfigMap.set(target,[fn]);
            }
        }
    })
    fn();
}